package org.leviatan.definator.core.admin;

import org.leviatan.common.keyutils.KeyGenerator;
import org.leviatan.definator.core.model.Table;
import org.leviatan.definator.core.model.impl.TableImpl;

public abstract class TableHandler {
	
	private static Table activeTable;
	
	public static boolean updateTable(Table updateInfo) {
		TableHandler.activeTable.setName(updateInfo.getName());
		RegistryHandler.saveTableModels();
		return true;
	}
	
	public static boolean updateTableName(String tblId, String newName) {
		Table tbl = RegistryHandler.getTableModel(tblId);
		if (tbl != null) {
			tbl.setName(newName);
			RegistryHandler.saveTableModels();
			return true;
		} else
			return false;
	}
	
	public static Table setTableModel(String tblId) {
		Table tbl = RegistryHandler.getTableModel(tblId);
		return TableHandler.setTableModel(tbl);
	}
	
	public static Table setTableModel(Table aTable) {
		TableHandler.activeTable = aTable;
		return TableHandler.activeTable;
	}
	
	public static Table getActiveTable() {
		return TableHandler.activeTable;
	}
	
	public static Table createNewTable() {
		Table newTbl = new TableImpl();
		newTbl.setId(KeyGenerator.generateUniqueKeyAsString());
		TableHandler.activeTable = newTbl;
		return TableHandler.activeTable;
	}
	
	public static void setTableName(String aName) {
		TableHandler.activeTable.setName(aName);
	}
	
	public static String getTableName() {
		return TableHandler.activeTable.getName();
	}
	
	public static String getTableId() {
		return TableHandler.activeTable.getId();
	}

}
